<?php
/*
 * Daum Hotel API Class
 * @version 0.3
 * @author Samgu Lee(cable8mm@gmail.com)
 * <code>
 * <?PHP
 * $DaumHotelApi = new DaumHotelApi('Hong Kong');
 * $DaumHotelApi->filter = array('strip', 'nullOff');
 * $DaumHotelApi->debug = true;
 * print_r($DaumHotelApi->get());
 * ?>
 * </code>
 */
class DaumHotelApi{
    const REQUEST_URL = 'http://apis.daum.net/tour/Hotel';
    const SEPARATE = '|';
    
    private $apikey = '[Your API Key]';
    private $type = 'xml';
    private $hotels = array();
    private $query = array();

    public $filter = array();
    public $debug = false;
    
    public function __construct($query)
    {
        $this->query = $this->query2Array($query);
        $this->getHotels();
    }
    
    public function get($visible = true)
    {
        if(!empty($this->filter))
            $this->applyFilter();
        
        return $visible? $this->hotels : '';
    }

    private function getHotels()
    {    
        $hotels = file_get_contents(self::REQUEST_URL.'?apikey='.$this->apikey.'&query='.$this->array2DaumQuery($this->query).'&output='.$this->type);
        $hotels_utf8 = $this->xml2UTF8($hotels);
        
        if($this->type == 'xml')
        {
            $hotelObject = simplexml_load_string($hotels_utf8, 'SimpleXMLElement', LIBXML_NOCDATA);
            $this->hotels= $this->simpleObject2Array($hotelObject->item);
        } else {
            $this->hotels = json_decode($hotels_utf8);
        }
    }
    
    private function xml2UTF8($str)
    {
        $str = iconv('EUC-KR', 'UTF-8', $str);
        return preg_replace('/EUC\-KR/', 'UTF-8', $str, 1);
    }
    
    private function applyFilter()
    {
        if(empty($this->filter))
            return;
        
        foreach($this->filter as $item)
        {
            if(!call_user_func(array($this, 'filter'.ucfirst($item)), $this->hotels))
                $this->error(3, 'filter'.ucfirst($item));
        }
        
        return;
    }
    
    private function filterStrip()
    {
        foreach($this->hotels as $key => $item)
        {
            $this->hotels[$key]['title'] = strip_tags($item['title']);
            $this->hotels[$key]['info'] = strip_tags($item['info']);
        }
    }
    
    private function filterNullOff()
    {
        foreach($this->hotels as $key => $item)
        {
            foreach($item as $key2 => $item2)
            {
                if(empty($item2))
                    unset($this->hotels[$key][$key2]);
            }
        }
    }
    
    private function simpleObject2Array($object)
    {
        $output = array();

        foreach($object as $key => $item)
            $output[] = (array) $item;

        return $output;
    }

    private function query2Array($query)
    {
        if(empty($query))
            return array();

        return preg_split('/[\,]+/', $query);
    }
    
    // @param Array $query
    private function array2DaumQuery($query)
    {
        if(empty($query))
            $this->error(2);
        
        $output = array();
        
        foreach($query as $item)
        {
            $output[] = iconv('UTF-8', 'EUC-KR', $item);
        }
        return urlencode(implode(self::SEPARATE, $output));
    }
    
    private function error($error, $param = '')
    {
        $output = '';
        if($this->debug)
        {
            $output .= 'Error Code : '.$error."\n";
            switch($error)
            {
                case 1:
                    $output .= 'Output type is wrong. xml or json must be.';
                break;
                case 2:
                    $output .= 'Query is empty.';
                break;
                case 3:
                    $output .= 'Filter(name : '.$param.') is not found.';
                break;
            }
            
            echo '<pre>'.$output.'</pre>';
            exit;
        }
    }
    
}
?> 